﻿using Common.BaseLibrary.Caching;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Xml;

namespace DepartmentWork.Platform.Commons
{
    public class CityManager
    {
        /// <summary>
        /// 线程锁
        /// </summary>
        private static object lockObj = new object();
        /// <summary>
        /// 城市工厂
        /// </summary>
        private static CityManager instance;

        public static CityManager Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (lockObj)
                    {
                        if (instance == null)
                        {
                            instance = new CityManager();
                        }
                    }
                }
                return instance;
            }
        }


        public string GetCityCodeByCityName(string cityName)
        {
            object weatherData = CacheManager.Instance.Get("WeatherData");
            DataSet weatherDataSet = new DataSet();
            if (weatherData == null)
            {
                weatherDataSet = ConvertXMLFileToDataSet(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"bin",
                     "WeatherConfig.xml"));

                CacheManager.Instance.Put("WeatherData", weatherDataSet);
            }
            else
            {
                weatherDataSet = (DataSet)weatherData;
            }

            DataRow dr= weatherDataSet.Tables[0].Select("Name='"+ cityName+"'")[0];

            return dr["Code"].ToString();

        }


        private async Task<string> GetWeatherByCityCode(string cityCode)
        {
            string url = @"http://www.weather.com.cn/data/cityinfo/" + cityCode + ".html";

            //Console.WriteLine("测试 web api 中的 Get All 方法 开始.....");

            HttpClientHandler handler = new HttpClientHandler();
            //{ AutomaticDecompression = System.Net.DecompressionMethods.GZip };

            using (HttpClient http = new HttpClient(handler))
            {
                //                        模拟 get  请求
                var response = await http.GetAsync(url);
                //可以在这里 判断 状态码
                //response.EnsureSuccessStatusCode(); 
                //Console.WriteLine("测试 Web Api 中的 GetALL 方法结束");

                return await response.Content.ReadAsStringAsync();
            }
        }

        /// <summary>
        /// 枚举通过值获取键
        /// </summary>
        /// <param name="enumType"></param>
        /// <param name="enumName"></param>
        /// <returns></returns>
        public int GetEnumValue(Type enumType, string enumName)
        {
            try
            {
                if (!enumType.IsEnum)
                    throw new ArgumentException("enumType必须是枚举类型");
                var values = Enum.GetValues(enumType);
                var ht = new Hashtable();
                foreach (var val in values)
                {
                    ht.Add(Enum.GetName(enumType, val), val);
                }
                return (int)ht[enumName];
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xmlFile"></param>
        /// <returns></returns>
        public DataSet ConvertXMLFileToDataSet(string xmlFile)
        {
            StringReader stream = null;
            XmlTextReader reader = null;
            try
            {
                XmlDocument xmld = new XmlDocument();
                xmld.Load(xmlFile);

                DataSet xmlDS = new DataSet();
                stream = new StringReader(xmld.InnerXml);
                //从stream装载到XmlTextReader
                reader = new XmlTextReader(stream);
                xmlDS.ReadXml(reader);
                //xmlDS.ReadXml(xmlFile);
                return xmlDS;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (reader != null) reader.Close();
            }
        }
    }
}